package com.service;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;

import com.util.ConstantUtil;
import com.utils.dbcp.ModelGenerator;

/**
 * 
 * @author zhanghusong
 */
public abstract class AbstractDateService {
	protected Logger logger = Logger.getLogger(this.getClass());
	// 采用的数据库连接
	protected static final String base = ConstantUtil.BASE;

private static Map<String, Integer> idmap = new HashMap<String, Integer>();
	
	private static Object idlock = new Object();
	
	protected int getTableId(String table)
	{
		synchronized (idlock) {
			if (!idmap.containsKey(table))
			{
				String sql = "select ifnull(max(id),1000) from " + table;
				ModelGenerator<Integer> mg = new ModelGenerator<Integer>(Integer.class);
				idmap.put(table, mg.generate(base, sql));
			}
			int id = idmap.get(table);
			id += 1;
			idmap.put(table, id);
			return id;
		}
	}
	protected String encodeDb(String value)
	{
		if (value == null || value.length() == 0)
			return "";
		return value.replace("'", "\\'");
	}
	protected boolean checkQueryDimension(String dimension, String key) {
		if (dimension == null)
			return false;
		String d[] = dimension.split(";");
		for (int n = 0; n < d.length; n++)
			if (d[n].equals(key))
				return true;
		return false;
	}
}

